package com.atguigu.guli.service.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Subject;
import com.atguigu.guli.service.edu.entity.excel.SubjectData;
import com.atguigu.guli.service.edu.listener.SubjectDataListener;
import com.atguigu.guli.service.edu.mapper.SubjectMapper;
import com.atguigu.guli.service.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author yz
 * @since 2022-07-18
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Resource
     SubjectMapper subjectMapper;
    @Resource
    SubjectDataListener subjectDataListener;
    @Override
    public void importSubjects(MultipartFile file) {
        //为解析的excel创建javaBean描述每一行数据
        try {
            EasyExcel.read(file.getInputStream()).sheet(0)
                    .head(SubjectData.class)
                    //.registerReadListener(new SubjectDataListener(subjectMapper))
                    .registerReadListener(subjectDataListener)
                    .doRead();
        } catch (IOException e) {
            throw new GuliException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR,e);
        }
    }

    @Override
    public List<Subject> getNestedSubjects() {
        //方法1
      //  List<Subject> subjects = baseMapper.selectList(null);
      //  //使用map来接收父节点对象
      //  HashMap<String, Subject> parents = new HashMap<>();
      //  //挑选出一级分类
      //  subjects.forEach(s->{
      //      if ("0".equals(s.getParentId())){//如果父节点id(初始化为0)=0则说明为一级分类
      //          parents.put(s.getId(),s);//将s集合对象设置给以id为key的map集合
      //      }
      //});
      //  //调出二级分类
      //  subjects.forEach(s->{
      //      if (!"0".equals(s.getParentId())){//说明为二级分类
      //          s.getParentId();//拿到二级目录的ID
      //
      //          Subject p =parents.get(s.getParentId());//获取到一级分类里的二级分类集合
      //          p.getChildren().add(s);//将二级
      //      }
      //  });
      //  return new ArrayList<>(parents.values());

        //方法2
        //查询整张表的数据
        List<Subject> subjects = this.list();
        //获取一级分类的list集合
        List<Subject> pSubjects = subjects.stream().filter(subject
                -> subject.getParentId().equals("0"))
                .collect(Collectors.toList());
        //遍历一级分类，挑选出所有课程分类属于该一级分类的children集合中
        pSubjects.forEach(pSubject->{
            List<Subject> children = subjects.stream()
                    .filter(subject -> subject.getParentId()
                            .equals(pSubject.getId()))
                    .collect(Collectors.toList());
            //将一级分类的子分类设置给children
            pSubject.setChildren(children);
        });
        return pSubjects;

    }
}
